import request from "../requests/gytconf";
import {setHeader,setParam} from '../requests/gytconf';

/**
 * 通过馆员账号获取登录信息，返回cookie
 * @param {string} user 用户名
 * @param {string} pwd 密码
 * @returns {string}
 */
export async function login(account,password){
  var res =await request({url:"login",method:"POST", data:{account,password}});  
  if(res.code==200){
      wx.setStorageSync('cookie', res.cookie);
      wx.setStorageSync('token', res.token);
      wx.setStorageSync('libcode', res.libcode);
      wx.setStorageSync('user', {account,password});
      setHeader("cookie",res.cookie);
      setHeader("token",res.token);
      setHeader("libcode",res.libcode);
      setHeader("account",account);
      setParam("account",account);
      setParam("libcode",res.libcode);
  }
  return res;
}
export async function relogin(){
  var user=wx.getStorageSync('user');
  if(user&&user.account&&user.password){
    var res=await login(user.account,user.password);
    return res;
  }
  return {code:404,msg:"从来没有登录过"};
}
export function personInfo(){
  return request({url:"personInfo",method:"get"});
}

export function loanReader(rdid){
  return request({url:"loanReader",method:"get",params:{rdid}});
}

export function loanBook(rdid,barcode,uid){
  return request({url:"loanBook",method:"get",params:{rdid,barcode,uid}});
}

export function returnBook(barcode,uid){
  return request({url:"returnBook",method:"get",params:{barcode,uid}});
}
export function returnBookByBlock(uid,block){
  return request({url:"ReturnBookByBlock",method:"get",params:{uid,block}});
}

export function getTodayTotal(){
  return request({url:"getTodayTotal",method:"get"});
}

export function getYearTotal(){
  return request({url:"getYearTotal",method:"get"});
}

export function isRegister(rdid){
  return request({url:"isRegister",method:"get",params:{rdid}});
}


export function addReader(idcard_res,phone){
    var data={
      id:idcard_res.id.text,
      name:idcard_res.name.text,
      gender:idcard_res.gender.text,
      nationality:idcard_res.nationality.text,
      birth:idcard_res.birth.text,
      address:idcard_res.address.text,
      phone
    }
    return request({url:"addReader",method:"POST",data});
}


export function newReaderHtml(){
  return request({url:"newReader",method:"get"});
}